--虽难持善
local suinanchishang = fk.CreateSkill {
    name = "mcdh_suinanchishang"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

suinanchishang:addEffect(fk.EventPhaseEnd,{
    can_trigger = function(self, event, target, player, data)
        return player == target and player.phase == Player.Play and player:hasSkill(self)
    end,
    on_cost  = function(self, event, target, player, data)
        local room = player.room
        local num = player:getHandcardNum()
        local hp = player.hp
        if num > hp then
            return room:askForSkillInvoke(player, self.name, data,"#suinanchishang1")
        elseif num == hp then
            return room:askForSkillInvoke(player, self.name, data, "#suinanchishang2")
        elseif num < hp then
            if #room:canMoveCardInBoard() > 0 then
                return room:askForSkillInvoke(player, self.name, data, "#suinanchishang3")
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local num = player:getHandcardNum()
        local hp = player.hp
        if num > hp then
            room:addPlayerMark(player,MarkEnum.AddMaxCardsInTurn,1)
        elseif num == hp then
            player:drawCards(2,self.name)
        elseif num < hp then
            local targets2 = table.map(table.filter(room.alive_players, function(p)
                return #p:getCardIds("ej") > 0 end), Util.IdMapper)
            if #targets2>0 then
                local to2 = room:askForChoosePlayers(player, targets2, 1, 1, "#suinanchishang3", self.name, true, true)
                if #to2 > 0 then
                    local cid = room:askForCardChosen(player, room:getPlayerById(to2[1]), "ej", self.name)
                    room:throwCard({cid}, self.name, room:getPlayerById(to2[1]), player)
                end
            end
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_suinanchishang"] = "虽难持善",
    [":mcdh_suinanchishang"] = "出牌阶段结束时,若你的手牌数与体力值相比:1.手牌数较多，则你可以令你本回合的手牌上限+1;2.相等,你可以摸两张牌;3.体力值较多,你可以弃置场上的一张牌。",
    ["#suinanchishang1"] = "虽难持善:令你本回合的手牌上限+1",
    ["#suinanchishang2"] = "虽难持善:摸两张牌",
    ["#suinanchishang3"] = "虽难持善:弃置场上的一张牌",
    ["#suinanchishang-discard"] = "虽难持善:弃置场上的一张牌。",
}

return suinanchishang